const app = getApp();

Page({
  data: {
    post: {},
    commentList: [],
    commentText: '',
    postId: null
  },

  onLoad: function(options) {
    this.setData({
      postId: options.id
    });
    this.loadPostDetail();
  },

  // 加载帖子详情
  loadPostDetail: function() {
    const { userStore } = getApp().globalData;
    const token = userStore.data.token || '';

    wx.request({
      url: `http://121.41.13.49:8081/post/list/detail/${this.data.postId}`,
      header: {
        'authentication': token
      },
      success: (res) => {
        if (res.data.code === 1) {
          this.setData({
            post: res.data.data
          });
          
          console.log('帖子数据：', this.data.post);
          console.log('评论数据：', this.data.post.comments);
        }
      }
    });
  },

  // 加载评论

  // 监听评论输入
  onCommentInput: function(e) {
    this.setData({
      commentText: e.detail.value
    });
  },

  // 提交评论
  submitComment: function() {
    const { userStore } = getApp().globalData;
    if (!userStore.data.isLogin) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    if (!this.data.commentText.trim()) {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: `http://121.41.13.49:8081/comment/add`,
      method: 'POST',
      data: {
        postId: this.data.postId,
        content: this.data.commentText
      },
      header: {
        'content-type': 'application/json',
        'authentication': userStore.data.token
      },
      success: (res) => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '评论成功',
            icon: 'success'
          });
          this.setData({
            commentText: ''
          });
          // 重新加载评论列表
          this.loadPostDetail();
          // 更新评论数
          this.setData({
            'post.comments': this.data.post.comments + 1
          });
        }
      }
    });
  },

  // 处理点赞
  handleLike: function() {
    // 这里添加点赞逻辑
  },

  // 预览图片
  previewImage: function(e) {
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      current,
      urls: this.data.post.images
    });
  }
}); 